package com.sinosoft.msf.base.supplier.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;

import com.sinosoft.msf.base.supplier.pojo.AppraisePojo;
import com.sinosoft.msf.base.supplier.pojo.OptVo;
import com.sinosoft.msf.base.supplier.pojo.PageParamVo;
import com.sinosoft.msf.base.supplier.pojo.RestEntity;
import com.sinosoft.msf.base.supplier.service.CommonBizService;
import com.sinosoft.msf.base.supplier.service.CompanyAppraiseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 供应商评价
 */
@RestController
@RequestMapping("/CompanyAppraise")
public class CompanyAppraiseController {
    private static final Logger log = LoggerFactory.getLogger(CompanyAppraiseController.class);
    @Autowired
    CompanyAppraiseService companyAppraiseService;
    @Autowired
    CommonBizService bizService;
    private OptVo curentOpt() {
        return bizService.queryCureUserInfo();
    }
    /**
     * 查询评价详情
     * @param appraisePojo
     * @return
     */
    @PostMapping(value = "/detail", name = "查询供应商评价详情")
    public RestEntity detail(@RequestBody AppraisePojo appraisePojo) {
        Validator.validateNotEmpty(appraisePojo.getProjectId(), "项目ID不可为空");
        Validator.validateNotEmpty(appraisePojo.getCompanyId(), "供应商ID不可为空");
        OptVo optVo = curentOpt();
        return RestEntity.data(companyAppraiseService.detail(appraisePojo,optVo));
    }

    /**
     * 评价保存
     * @param appraisePojo
     * @return
     */
    @PostMapping(value = "/save", name = "保存供应商评价")
    public RestEntity save(@RequestBody AppraisePojo appraisePojo) {
        Validator.validateNotEmpty(appraisePojo.getProjectId(), "项目ID不可为空");
        Validator.validateNotEmpty(appraisePojo.getCompanyId(), "供应商ID不可为空");
        OptVo optVo = curentOpt();
        companyAppraiseService.save(appraisePojo, optVo);
        return RestEntity.success();
    }

    /**
     * 评价列表查询
     * @return
     */
    @PostMapping(value = "/query", name = "保存供应商评价")
    public RestEntity query(@RequestBody PageParamVo paramVo) {

        JSONObject paramData = paramVo.getParamData();
        String createdDate = paramData.getString("createdDate");
        if (StrUtil.isNotEmpty(createdDate)) {
            String[] split = createdDate.split(",");
            if(split.length==2){
                paramData.put("createdDateStart", split[0]);
                paramData.put("createdDateEnd", split[1]);
            }
        }
        OptVo optVo = curentOpt();
        return RestEntity.data(companyAppraiseService.query(paramVo, optVo));
    }
    /**
     * 评价列表查询
     * @return
     */
    @PostMapping(value = "/del", name = "删除评论")
    public RestEntity del(@RequestBody AppraisePojo appraisePojo) {
        Validator.validateNotEmpty(appraisePojo.getRecordId(), "评论ID不可为空");

        OptVo optVo = curentOpt();
        companyAppraiseService.del(appraisePojo,optVo);
        return RestEntity.success();
    }

}
